﻿using System;
using System.Collections.Generic;
using System.Transactions;
using System.Linq;
using System.Data.Linq;
using Hxsoft.AppV1;

namespace Hxsoft.AppV1.Module
{
	public partial class ModuleArticle : ICreator { }




	/// <summary>
	/// ModuleArticles 类。
	/// </summary>
	public static partial class ModuleArticles
	{
		public const string INFO_TYPE = "ModuleArticle";


		#region GetItem
		/// <summary>
		/// 根据标识获取指定的模块文章。
		/// </summary>
		/// <param name="o">模块文章集合。</param>
		/// <param name="moduleId">获取的模块文章标识 ModuleId。</param>
		/// <param name="articleId">获取的模块文章标识 ArticleId。</param>
		/// <returns>获取的信息项。</returns>
		public static ModuleArticle GetItem(this System.Data.Linq.Table<ModuleArticle> o, int moduleId, int articleId)
		{
			return o.SingleOrDefault(p => p.ModuleId == moduleId && p.ArticleId == articleId);
		}

		public static ModuleArticle GetItem(int moduleId, int articleId)
		{
			GenericModuleDataContext dc = new GenericModuleDataContext();
			dc.ObjectTrackingEnabled = false;
			return dc.ModuleArticles.GetItem(moduleId, articleId);
		}
		#endregion



		#region Exists
		/// <summary>
		/// 验证指定的模块文章是否已经存在。
		/// </summary>
		/// <param name="o">模块文章集合。</param>
		/// <param name="moduleId">验证的模块文章标识 ModuleId。</param>
		/// <param name="articleId">验证的模块文章标识 ArticleId。</param>
		/// <returns>布尔值表示指定的模块文章是否已经存在。</returns>
		public static bool Exists(this System.Data.Linq.Table<ModuleArticle> o, int moduleId, int articleId)
		{
			return o.Any(p => p.ModuleId == moduleId && p.ArticleId == articleId);
		}

		public static bool Exists(int moduleId, int articleId)
		{
			GenericModuleDataContext dc = new GenericModuleDataContext();
			dc.ObjectTrackingEnabled = false;
			return dc.ModuleArticles.Exists(moduleId, articleId);
		}
		#endregion
	}



	/// <summary>
	/// ModuleArticles 类操作部分。
	/// </summary>
	public static partial class ModuleArticles
	{
		#region Release
		/// <summary>
		/// 发布模块文章。
		/// </summary>
		/// <param name="releasedModuleArticle">发布的模块文章。</param>
		/// <returns>发布的模块文章。</returns>
		public static ModuleArticle Release(ModuleArticle releasedModuleArticle)
		{
			GenericModuleDataContext dc = new GenericModuleDataContext();

			ModuleArticle moduleArticle = releasedModuleArticle.ToNewEntity<ModuleArticle>();
			moduleArticle.CreatorId = Users.CurrentUserId;
			moduleArticle.ReleaseDate = DateTime.Now;
			moduleArticle.ArticleOrder = 0;
			moduleArticle.IsEffective = false;
			moduleArticle.ChannelIds = "";
			moduleArticle.RegionIds = "";

			GetReleaseRight(dc, moduleArticle, true);	// 权限验证

			dc.ModuleArticles.InsertOnSubmit(moduleArticle);
			dc.SubmitChanges();

			// 记录日志
			Guid? resourceId = dc.Modules.GetArticleReleaseResourceId(releasedModuleArticle.ModuleId);
			Logs.Insert(dc, resourceId, (int)Modules.ArticleReleasePermissionCode.Release, INFO_TYPE, moduleArticle.ArticleId.ToString(), Modules.ArticleReleasePermissionCode.Release.ToString(), moduleArticle.ModuleId.ToString());

			// 返回只读数据
			return releasedModuleArticle.FillByEntity(moduleArticle);
		}

		public static ModuleArticle Release(Article insertedArticle, int moduleId)
		{
			using (TransactionScope ts = new TransactionScope())
			{
				Article article = Articles.Insert(insertedArticle);

				ModuleArticle releasedModuleArticle = new ModuleArticle();
				releasedModuleArticle.ModuleId = moduleId;
				releasedModuleArticle.ArticleId = article.ArticleId;
				releasedModuleArticle = Release(releasedModuleArticle);

				ts.Complete();

				return releasedModuleArticle;
			}
		}
		#endregion



		#region Setting
		private static readonly HashSet<string> _settedExcludeColumns = typeof(ModuleArticle).GetEntityColumnNames(new string[] {
				"ModuleId",
				"ArticleId",
				"CreatorId",
				"ReleaseDate"
			}, true);

		/// <summary>
		/// 设置模块文章。
		/// </summary>
		/// <param name="settedModuleArticle">设置的模块文章。</param>
		/// <returns>设置的模块文章。</returns>
		public static ModuleArticle Setting(ModuleArticle settedModuleArticle)
		{
			GenericModuleDataContext dc = new GenericModuleDataContext();

			ModuleArticle moduleArticle = dc.ModuleArticles.GetItem(settedModuleArticle.ModuleId, settedModuleArticle.ArticleId);
			if (moduleArticle == null)
				throw new ObjectNotExistsException(Res.Operation_ObjectNotExists);

			GetSettingRight(dc, moduleArticle, true);	// 权限验证

			moduleArticle.FillByEntity(settedModuleArticle, EntityColumnsSetType.Exclude, _settedExcludeColumns);

			try
			{
				dc.SubmitChanges(ConflictMode.ContinueOnConflict);
			}
			catch (ChangeConflictException e)
			{
				foreach (ObjectChangeConflict occ in dc.ChangeConflicts)
				{
					occ.Resolve(RefreshMode.KeepCurrentValues);
				}
			}

			// 记录日志
			Guid? resourceId = dc.Modules.GetArticleReleaseResourceId(settedModuleArticle.ModuleId);
			Logs.Insert(dc, resourceId, (int)Modules.ArticleReleasePermissionCode.Setting, INFO_TYPE, moduleArticle.ArticleId.ToString(), Modules.ArticleReleasePermissionCode.Setting.ToString(), moduleArticle.ModuleId.ToString());

			// 返回只读数据
			return settedModuleArticle.FillByEntity(moduleArticle);
		}
		#endregion



		#region SetEffectiveState
		/// <summary>
		/// 设置信息的有效状态。
		/// </summary>
		/// <param name="articleId">设置的信息标识。</param>
		public static void SetEffectiveState(int moduleId, int articleId)
		{
			GenericModuleDataContext dc = new GenericModuleDataContext();

			ModuleArticle moduleArticle = dc.ModuleArticles.GetItem(moduleId, articleId);
			if (moduleArticle == null)
				throw new ObjectNotExistsException(Res.Operation_ObjectNotExists);

			GetSetEffectiveStateRight(dc, moduleArticle, true);	// 权限验证

			moduleArticle.IsEffective = !moduleArticle.IsEffective;
			try
			{
				dc.SubmitChanges(ConflictMode.ContinueOnConflict);
			}
			catch (ChangeConflictException e)
			{
				foreach (ObjectChangeConflict occ in dc.ChangeConflicts)
				{
					occ.Resolve(RefreshMode.KeepCurrentValues);
				}
			}

			// 记录日志
			Guid? resourceId = dc.Modules.GetArticleReleaseResourceId(moduleId);
			Logs.Insert(dc, resourceId, (int)Modules.ArticleReleasePermissionCode.Setting, INFO_TYPE, moduleArticle.ArticleId.ToString(), Modules.ArticleReleasePermissionCode.Setting.ToString() + "/EffectiveState", moduleArticle.ModuleId.ToString());
		}

		/// <summary>
		/// 设置信息的有效状态。
		/// </summary>
		/// <param name="moduleId">设置的信息标识。</param>
		/// <param name="articleId">设置的信息标识。</param>
		/// <param name="state">设置的状态。</param>
		public static void SetEffectiveState(int moduleId, int articleId, bool state)
		{
			GenericModuleDataContext dc = new GenericModuleDataContext();

			ModuleArticle moduleArticle = dc.ModuleArticles.GetItem(moduleId, articleId);
			if (moduleArticle == null)
				throw new ObjectNotExistsException(Res.Operation_ObjectNotExists);

			GetSetEffectiveStateRight(dc, moduleArticle, true);	// 权限验证

			if (moduleArticle.IsEffective == state)
				return;

			moduleArticle.IsEffective = state;
			try
			{
				dc.SubmitChanges(ConflictMode.ContinueOnConflict);
			}
			catch (ChangeConflictException e)
			{
				foreach (ObjectChangeConflict occ in dc.ChangeConflicts)
				{
					occ.Resolve(RefreshMode.KeepCurrentValues);
				}
			}

			// 记录日志
			Guid? resourceId = dc.Modules.GetArticleReleaseResourceId(moduleId);
			Logs.Insert(dc, resourceId, (int)Modules.ArticleReleasePermissionCode.Setting, INFO_TYPE, moduleArticle.ArticleId.ToString(), Modules.ArticleReleasePermissionCode.Setting.ToString() + "/EffectiveState", moduleArticle.ModuleId.ToString());
		}
		#endregion



		#region Delete
		/// <summary>
		/// 移除模块文章。
		/// </summary>
		/// <param name="moduleId">移除的模块文章标识 ModuleId。</param>
		/// <param name="articleId">移除的模块文章标识 ArticleId。</param>
		public static void Remove(int moduleId, int articleId)
		{
			GenericModuleDataContext dc = new GenericModuleDataContext();

			ModuleArticle moduleArticle = dc.ModuleArticles.GetItem(moduleId, articleId);
			if (moduleArticle == null)
				throw new ObjectNotExistsException(Res.Operation_ObjectNotExists);

			GetRemoveRight(dc, moduleArticle, true);	// 权限验证

			Delete(dc, moduleArticle);

			dc.SubmitChanges();

			// 记录日志
			Guid? resourceId = dc.Modules.GetArticleReleaseResourceId(moduleId);
			Logs.Insert(dc, resourceId, (int)Modules.ArticleReleasePermissionCode.Remove, INFO_TYPE, moduleArticle.ArticleId.ToString(), Modules.ArticleReleasePermissionCode.Remove.ToString(), moduleArticle.ModuleId.ToString());
		}

		#region internal Delete
		internal static void Delete(GenericModuleDataContext dc, ModuleArticle moduleArticle)
		{
			dc.ModuleArticles.DeleteOnSubmit(moduleArticle);
		}

		internal static void DeleteByParent(GenericModuleDataContext dc, Module parent)
		{
			IEnumerable<ModuleArticle> e = parent.ModuleArticles.AsEnumerable();
			foreach (ModuleArticle i in e)
				Delete(dc, i);
		}

		internal static void DeleteByParent(GenericModuleDataContext dc, Article parent)
		{
			IEnumerable<ModuleArticle> e = parent.ModuleArticles.AsEnumerable();
			foreach (ModuleArticle i in e)
				Delete(dc, i);
		}
		#endregion
		#endregion
	}



	/// <summary>
	/// ModuleArticles 类对应操作权限部分。
	/// </summary>
	public static partial class ModuleArticles
	{
		#region GetReleaseRight
		/// <summary>
		/// 获取当前用户是否有发布模块文章的权限。
		/// </summary>
		/// <param name="dc">数据容器。</param>
		/// <param name="moduleArticle">发布的模块文章对象。</param>
		/// <param name="triggerException">当未通过权限验证的时候是否引发异常。</param>
		/// <returns>布尔值表示当前用户是否有发布模块文章的权限。</returns>
		public static bool GetReleaseRight(GenericModuleDataContext dc, ModuleArticle moduleArticle, bool triggerException)
		{
			string userName = Users.CurrentUserName;
			int permissionCode = (int)Modules.ArticleReleasePermissionCode.Release;
			Guid? resourceId = dc.Modules.GetArticleReleaseResourceId(dc.Modules.GetItem(moduleArticle.ModuleId));
			if (!resourceId.HasValue)
				throw new ObjectNotExistsException(Res.Resources_NotExistsResource);

			bool b = dc.AccessController.GetUserIsHasPermission<ModuleArticle>(userName, moduleArticle, resourceId.Value, permissionCode);

			if (triggerException && !b)
				throw new PermissionException(userName, moduleArticle, resourceId.Value, permissionCode, Res.PermissionExceptionMessage);
			return b;
		}

		/// <summary>
		/// 获取当前用户是否有发布模块文章的权限。
		/// </summary>
		/// <param name="moduleArticle">发布的模块文章对象。</param>
		/// <param name="triggerException">当未通过权限验证的时候是否引发异常。</param>
		/// <returns>布尔值表示当前用户是否有发布模块文章的权限。</returns>
		public static bool GetReleaseRight(ModuleArticle moduleArticle, bool triggerException)
		{
			return GetReleaseRight(new GenericModuleDataContext(), moduleArticle, triggerException);
		}

		/// <summary>
		/// 获取当前用户是否有发布模块文章的权限。
		/// </summary>
		/// <param name="moduleArticle">发布的模块文章对象。</param>
		/// <returns>布尔值表示当前用户是否有发布模块文章的权限。</returns>
		public static bool GetReleaseRight(ModuleArticle moduleArticle)
		{
			return GetReleaseRight(moduleArticle, false);
		}
		#endregion



		#region GetSettingRight
		/// <summary>
		/// 获取当前用户是否有设置模块文章的权限。
		/// </summary>
		/// <param name="dc">数据容器。</param>
		/// <param name="moduleArticle">设置的模块文章对象。</param>
		/// <param name="triggerException">当未通过权限验证的时候是否引发异常。</param>
		/// <returns>布尔值表示当前用户是否有设置模块文章的权限。</returns>
		public static bool GetSettingRight(GenericModuleDataContext dc, ModuleArticle moduleArticle, bool triggerException)
		{
			string userName = Users.CurrentUserName;
			int permissionCode = (int)Modules.ArticleReleasePermissionCode.Setting;
			Guid? resourceId = dc.Modules.GetArticleReleaseResourceId(dc.Modules.GetItem(moduleArticle.ModuleId));
			if (!resourceId.HasValue)
				throw new ObjectNotExistsException(Res.Resources_NotExistsResource);

			bool b = dc.AccessController.GetUserIsHasPermission<ModuleArticle>(userName, moduleArticle, resourceId.Value, permissionCode);

			if (triggerException && !b)
				throw new PermissionException(userName, moduleArticle, resourceId.Value, permissionCode, Res.PermissionExceptionMessage);
			return b;
		}

		/// <summary>
		/// 获取当前用户是否有设置模块文章的权限。
		/// </summary>
		/// <param name="moduleArticle">设置的模块文章对象。</param>
		/// <param name="triggerException">当未通过权限验证的时候是否引发异常。</param>
		/// <returns>布尔值表示当前用户是否有设置模块文章的权限。</returns>
		public static bool GetSettingRight(ModuleArticle moduleArticle, bool triggerException)
		{
			return GetSettingRight(new GenericModuleDataContext(), moduleArticle, triggerException);
		}

		/// <summary>
		/// 获取当前用户是否有设置模块文章的权限。
		/// </summary>
		/// <param name="moduleArticle">设置的模块文章对象。</param>
		/// <returns>布尔值表示当前用户是否有设置模块文章的权限。</returns>
		public static bool GetSettingRight(ModuleArticle moduleArticle)
		{
			return GetSettingRight(moduleArticle, false);
		}



		public static bool GetSetEffectiveStateRight(GenericModuleDataContext dc, ModuleArticle moduleArticle, bool triggerException)
		{
			return GetSettingRight(dc, moduleArticle, triggerException);
		}

		public static bool GetSetEffectiveStateRight(ModuleArticle moduleArticle, bool triggerException)
		{
			return GetSetEffectiveStateRight(new GenericModuleDataContext(), moduleArticle, triggerException);
		}

		public static bool GetSetEffectiveStateRight(ModuleArticle moduleArticle)
		{
			return GetSetEffectiveStateRight(moduleArticle, false);
		}
		#endregion



		#region GetRemoveRight
		/// <summary>
		/// 获取当前用户是否有移除模块文章的权限。
		/// </summary>
		/// <param name="dc">数据容器。</param>
		/// <param name="moduleArticle">移除的模块文章对象。</param>
		/// <param name="triggerException">当未通过权限验证的时候是否引发异常。</param>
		/// <returns>布尔值表示当前用户是否有移除模块文章的权限。</returns>
		public static bool GetRemoveRight(GenericModuleDataContext dc, ModuleArticle moduleArticle, bool triggerException)
		{
			string userName = Users.CurrentUserName;
			int permissionCode = (int)Modules.ArticleReleasePermissionCode.Remove;
			Guid? resourceId = dc.Modules.GetArticleReleaseResourceId(dc.Modules.GetItem(moduleArticle.ModuleId));
			if (!resourceId.HasValue)
				throw new ObjectNotExistsException(Res.Resources_NotExistsResource);

			bool b = dc.AccessController.GetUserIsHasPermission<ModuleArticle>(userName, moduleArticle, resourceId.Value, permissionCode);

			if (triggerException && !b)
				throw new PermissionException(userName, moduleArticle, resourceId.Value, permissionCode, Res.PermissionExceptionMessage);
			return b;
		}

		/// <summary>
		/// 获取当前用户是否有移除模块文章的权限。
		/// </summary>
		/// <param name="moduleArticle">移除的模块文章对象。</param>
		/// <param name="triggerException">当未通过权限验证的时候是否引发异常。</param>
		/// <returns>布尔值表示当前用户是否有移除模块文章的权限。</returns>
		public static bool GetRemoveRight(ModuleArticle moduleArticle, bool triggerException)
		{
			return GetRemoveRight(new GenericModuleDataContext(), moduleArticle, triggerException);
		}

		/// <summary>
		/// 获取当前用户是否有移除模块文章的权限。
		/// </summary>
		/// <param name="moduleArticle">移除的模块文章对象。</param>
		/// <returns>布尔值表示当前用户是否有移除模块文章的权限。</returns>
		public static bool GetRemoveRight(ModuleArticle moduleArticle)
		{
			return GetRemoveRight(moduleArticle, false);
		}
		#endregion



		#region GetReadRight
		/// <summary>
		/// 获取当前用户是否有阅读模块文章的权限。
		/// </summary>
		/// <param name="dc">数据容器。</param>
		/// <param name="moduleArticle">阅读的模块文章对象。</param>
		/// <param name="triggerException">当未通过权限验证的时候是否引发异常。</param>
		/// <returns>布尔值表示当前用户是否有阅读模块文章的权限。</returns>
		public static bool GetReadRight(GenericModuleDataContext dc, ModuleArticle moduleArticle, bool triggerException)
		{
			string userName = Users.CurrentUserName;
			int permissionCode = (int)Modules.ArticleReleasePermissionCode.Read;
			Guid? resourceId = dc.Modules.GetArticleReleaseResourceId(dc.Modules.GetItem(moduleArticle.ModuleId));
			if (!resourceId.HasValue)
				throw new ObjectNotExistsException(Res.Resources_NotExistsResource);

			bool b = dc.AccessController.GetUserIsHasPermission<ModuleArticle>(userName, moduleArticle, resourceId.Value, permissionCode);

			if (triggerException && !b)
				throw new PermissionException(userName, moduleArticle, resourceId.Value, permissionCode, Res.PermissionExceptionMessage);
			return b;
		}

		/// <summary>
		/// 获取当前用户是否有阅读模块文章的权限。
		/// </summary>
		/// <param name="moduleArticle">阅读的模块文章对象。</param>
		/// <param name="triggerException">当未通过权限验证的时候是否引发异常。</param>
		/// <returns>布尔值表示当前用户是否有阅读模块文章的权限。</returns>
		public static bool GetReadRight(ModuleArticle moduleArticle, bool triggerException)
		{
			return GetReadRight(new GenericModuleDataContext(), moduleArticle, triggerException);
		}

		/// <summary>
		/// 获取当前用户是否有阅读模块文章的权限。
		/// </summary>
		/// <param name="moduleArticle">阅读的模块文章对象。</param>
		/// <returns>布尔值表示当前用户是否有阅读模块文章的权限。</returns>
		public static bool GetReadRight(ModuleArticle moduleArticle)
		{
			return GetReadRight(moduleArticle, false);
		}
		#endregion


        #region GetReadRightByUser
        /// <summary>
        /// 获取当前用户是否有阅读模块文章的权限。
        /// </summary>
        /// <param name="dc">数据容器。</param>
        /// <param name="moduleArticle">阅读的模块文章对象。</param>
        /// <param name="triggerException">当未通过权限验证的时候是否引发异常。</param>
        /// <returns>布尔值表示当前用户是否有阅读模块文章的权限。</returns>
        public static bool GetReadRight(GenericModuleDataContext dc, ModuleArticle moduleArticle,string  userName, bool triggerException)
        {            
            int permissionCode = (int)Modules.ArticleReleasePermissionCode.Read;
            Guid? resourceId = dc.Modules.GetArticleReleaseResourceId(dc.Modules.GetItem(moduleArticle.ModuleId));
            if (!resourceId.HasValue)
                throw new ObjectNotExistsException(Res.Resources_NotExistsResource);

            bool b = dc.AccessController.GetUserIsHasPermission<ModuleArticle>(userName, moduleArticle, resourceId.Value, permissionCode);

            if (triggerException && !b)
                throw new PermissionException(userName, moduleArticle, resourceId.Value, permissionCode, Res.PermissionExceptionMessage);
            return b;
        }

        /// <summary>
        /// 获取当前用户是否有阅读模块文章的权限。
        /// </summary>
        /// <param name="moduleArticle">阅读的模块文章对象。</param>
        /// <param name="triggerException">当未通过权限验证的时候是否引发异常。</param>
        /// <returns>布尔值表示当前用户是否有阅读模块文章的权限。</returns>
        public static bool GetReadRight(ModuleArticle moduleArticle,string  userName, bool triggerException)
        {
            return GetReadRight(new GenericModuleDataContext(), moduleArticle,userName, triggerException);
        }

        /// <summary>
        /// 获取当前用户是否有阅读模块文章的权限。
        /// </summary>
        /// <param name="moduleArticle">阅读的模块文章对象。</param>
        /// <returns>布尔值表示当前用户是否有阅读模块文章的权限。</returns>
        public static bool GetReadRight(ModuleArticle moduleArticle, string userName)
        {
            return GetReadRight(moduleArticle,userName, false);
        }
        #endregion
	}



	
	/// <summary>
	/// ModuleArticles 类后台管理部分。
	/// </summary>
	public static partial class ModuleArticles
	{
		public static bool GetManageListRight()
		{
			GenericModuleDataContext dc = new GenericModuleDataContext();
			return dc.AccessController.GetUserIsHasCategoryPermission<object>(Users.CurrentUserName, null, Modules.ArticleReleaseResourceCategoryId, (int)Modules.ArticleReleasePermissionCode.Release, App.Context.CurrentSiteId);
		}

		public static bool GetManageListRight(int? moduleId)
		{
			if (!moduleId.HasValue) return true;

			GenericModuleDataContext dc = new GenericModuleDataContext();
			Guid? resourceId = dc.Modules.GetArticleReleaseResourceId(moduleId.Value);
			if (resourceId == null) return false;
			return dc.AccessController.GetUserIsHasPermission<object>(Users.CurrentUserName, null, resourceId.Value, (int)Modules.ArticleReleasePermissionCode.Release, Sites.CurrentSiteId);
		}

		public static bool GetManageListRight(int? siteId,int? moduleId)
		{
			if (!moduleId.HasValue) return true;

			GenericModuleDataContext dc = new GenericModuleDataContext();
			Guid? resourceId = dc.Modules.GetArticleReleaseResourceId(moduleId.Value);
			if (resourceId == null) return false;
			return dc.AccessController.GetUserIsHasPermission<object>(Users.CurrentUserName, null, resourceId.Value, (int)Modules.ArticleReleasePermissionCode.Release, siteId);
		}

		public static bool GetManageListRight(int? siteId, int? moduleId,string userName)
		{
			if (!moduleId.HasValue) return true;

			GenericModuleDataContext dc = new GenericModuleDataContext();
			Guid? resourceId = dc.Modules.GetArticleReleaseResourceId(moduleId.Value);
			if (resourceId == null) return false;
			return dc.AccessController.GetUserIsHasPermission<object>(userName, null, resourceId.Value, (int)Modules.ArticleReleasePermissionCode.Release, siteId);
		}

		public static bool GetManageListRight(int? moduleId,string userName)
		{
			if (!moduleId.HasValue) return true;

			GenericModuleDataContext dc = new GenericModuleDataContext();
			Guid? resourceId = dc.Modules.GetArticleReleaseResourceId(moduleId.Value);
			if (resourceId == null) return false;
			return dc.AccessController.GetUserIsHasPermission<object>(userName, null, resourceId.Value, (int)Modules.ArticleReleasePermissionCode.Release, Sites.CurrentSiteId);
		}

		public static bool GetRootManageListRight(int siteId)
		{
			GenericModuleDataContext dc = new GenericModuleDataContext();
			Guid? resourceId = dc.Modules.GetArticleReleaseRootResourceId(siteId);
			if (resourceId == null) return false;
			return dc.AccessController.GetUserIsHasPermission<object>(Users.CurrentUserName, null, resourceId.Value, (int)Modules.ArticleReleasePermissionCode.Release, siteId);
		}

		public static bool GetRootManageListRight(int siteId,string userName)
		{
			GenericModuleDataContext dc = new GenericModuleDataContext();
			Guid? resourceId = dc.Modules.GetArticleReleaseRootResourceId(siteId);
			if (resourceId == null) return false;
			return dc.AccessController.GetUserIsHasPermission<object>(userName, null, resourceId.Value, (int)Modules.ArticleReleasePermissionCode.Release, siteId);
		}



		public class ModuleArticleForManageList
		{
			public int ModuleId { get; set; }
			public int ArticleId { get; set; }
			public string Title { get; set; }
			public bool ModuleArticleIsEffective { get; set; }
			public bool ArticleIsEffective { get; set; }
			public DateTime ReleaseDate { get; set; }
			public int ArticleOrder { get; set; }
			public Guid? CreatorId { get; set; }
			public string ChannelIds { get; set; }
			public bool IsRecommend { get; set; }
			public string RegionIds { get; set; }
			public string CreatorName
			{
				get { return CreatorId.HasValue ? Users.GetUserName(CreatorId.Value) : null; }
			}
		}

		public static IQueryable<ModuleArticleForManageList> GetModuleArticleQuery(GenericModuleDataContext dc, int? moduleId, int? articleId, bool? articleIsEffective, bool? moduleArticleIsEffective, string userName, DateTime? startTime, DateTime? endTime, string keyword)
		{
			var q = from p in dc.ViewModuleArticles
					select p;

			if (moduleId.HasValue)
				q = q.Where(p => p.ModuleId == moduleId.Value);

			if (articleId.HasValue)
				q = q.Where(p => p.ArticleId == articleId.Value);

			if (articleIsEffective.HasValue)
				q = q.Where(p => p.ArticleIsEffective == articleIsEffective.Value);

			if (moduleArticleIsEffective.HasValue)
				q = q.Where(p => p.ModuleArticleIsEffective == moduleArticleIsEffective.Value);

			if (!string.IsNullOrEmpty(userName))
			{
				Guid? userId = Users.GetUserId(userName);
				if (userId.HasValue)
					q = q.Where(p => p.ArticleCreatorId == userId.Value);
				else
					q = q.Where(p => 1 == 0);
			}

			if (startTime.HasValue)
				q = q.Where(p => p.ModuleArticleReleaseDate >= startTime.Value);
			if (endTime.HasValue)
				q = q.Where(p => p.ModuleArticleReleaseDate <= endTime.Value);

			if (!string.IsNullOrEmpty(keyword))
				q = q.Where(p => p.Title.Contains(keyword) || p.ViceTitle.Contains(keyword) || p.Keywords.Contains(keyword) || p.ArtSource.Contains(keyword));

			var q2 = q.Select(p => new ModuleArticleForManageList
			{
				ModuleId = p.ModuleId,
				ArticleId = p.ArticleId,
				Title = p.Title,
				ModuleArticleIsEffective = p.ModuleArticleIsEffective,
				ArticleIsEffective = p.ArticleIsEffective,
				ReleaseDate = p.ArticleReleaseDate,
				ArticleOrder = p.ArticleOrder,
				CreatorId = p.ArticleCreatorId,
				ChannelIds = p.ChannelIds,
				IsRecommend = p.IsRecommend,
				RegionIds = p.RegionIds
			}).OrderByDescending(p => p.ArticleOrder).OrderByDescending(p => p.ReleaseDate);
			
			return q2;
		}



		public static PaginationInfo<List<ModuleArticleForManageList>> GetPaginationManageList(int? moduleId, int? articleId, bool? articleIsEffective, bool? moduleArticleIsEffective, string userName, DateTime? startTime, DateTime? endTime, string keyword, int pageSize, int currentPage)
		{
			GenericModuleDataContext dc = new GenericModuleDataContext();
			dc.ObjectTrackingEnabled = false;

			IQueryable<ModuleArticleForManageList> q= GetModuleArticleQuery(dc,  moduleId,  articleId,  articleIsEffective,  moduleArticleIsEffective, userName,  startTime,  endTime, keyword);

			PaginationInfo<List<ModuleArticleForManageList>> pageInfo = new PaginationInfo<List<ModuleArticleForManageList>>();
			pageInfo.PageSize = pageSize;
			pageInfo.CurrentPage = currentPage;
			pageInfo.RecordCount = q.Count();
			pageInfo.Records = q.Skip((currentPage - 1) * pageSize).Take(pageSize).ToList();
			return pageInfo;
		}

		public static List<ModuleArticleForManageList> GetModuleArticleList(int? moduleId, int? articleId, bool? articleIsEffective, bool? moduleArticleIsEffective, string userName, DateTime? startTime, DateTime? endTime, string keyword, int? loadCount)
		{
			GenericModuleDataContext dc = new GenericModuleDataContext();
			dc.ObjectTrackingEnabled = false;

			IQueryable<ModuleArticleForManageList> q = GetModuleArticleQuery(dc, moduleId, articleId, articleIsEffective, moduleArticleIsEffective, userName, startTime, endTime, keyword);

			if (loadCount.HasValue)
				return q.Take(loadCount.Value).ToList();

			return q.ToList();
		}



		public class ModuleArticleForSelector
		{
			public int ModuleId { get; set; }
			public string ModuleName { get; set; }
			public int ArticleId { get; set; }
			public string Title { get; set; }
			public bool ModuleArticleIsEffective { get; set; }
			public bool ArticleIsEffective { get; set; }
			public DateTime ReleaseDate { get; set; }
			public int ArticleOrder { get; set; }
			public string ModuleCode { get; set; }
			public string ChannelIds { get; set; }
			public bool IsRecommend { get; set; }
			public string RegionIds { get; set; }
		}

		public static PaginationInfo<List<ModuleArticleForSelector>> GetPaginationSelectorList(int? siteId, int? articleId, string moduleCode, string keyword, int pageSize, int currentPage)
		{
			GenericModuleDataContext dc = new GenericModuleDataContext();
			dc.ObjectTrackingEnabled = false;

			var q = from p in dc.ViewModuleArticles
					select p;

			if (siteId.HasValue)
				q = q.Where(p => p.SiteId == siteId.Value);
			
			if (articleId.HasValue)
				q = q.Where(p => p.ArticleId == articleId.Value);

			if (!string.IsNullOrEmpty(moduleCode))
				q = q.Where(p => p.ModuleCode.Contains(moduleCode));

			if (!string.IsNullOrEmpty(keyword))
				q = q.Where(p => p.Title.Contains(keyword) || p.ViceTitle.Contains(keyword) || p.Keywords.Contains(keyword) || p.ArtSource.Contains(keyword));

			var q2 = q.Select(p => new ModuleArticleForSelector
			{
				ModuleId = p.ModuleId,
				ModuleName = p.ModuleName,
				ArticleId = p.ArticleId,
				Title = p.Title,
				ModuleArticleIsEffective = p.ModuleArticleIsEffective,
				ArticleIsEffective = p.ArticleIsEffective,
				ReleaseDate = p.ArticleReleaseDate,
				ArticleOrder = p.ArticleOrder,
				ModuleCode = p.ModuleCode,
				ChannelIds = p.ChannelIds,
				IsRecommend = p.IsRecommend,
				RegionIds = p.RegionIds
			}).OrderByDescending(p => p.ArticleOrder).OrderByDescending(p => p.ReleaseDate);

			PaginationInfo<List<ModuleArticleForSelector>> pageInfo = new PaginationInfo<List<ModuleArticleForSelector>>();
			pageInfo.PageSize = pageSize;
			pageInfo.CurrentPage = currentPage;
			pageInfo.RecordCount = q2.Count();
			pageInfo.Records = q2.Skip((currentPage - 1) * pageSize).Take(pageSize).ToList();
			return pageInfo;
		}
	}

	[Serializable]
	public class ModuleArticleId
	{
		public int ModuleId { get; set; }
		public int ArticleId { get; set; }
	}
}